using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Xml;
using Microsoft.SharePoint.Publishing;

namespace Safira.Tools.Sharepoint.Spome
{
    public class ESPPage
    {
        private PublishingPage _page;

        /// <summary>
        /// Gets the page.
        /// </summary>
        /// <value>The page.</value>
        public PublishingPage Page
        {
            get { return _page; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ESPPage"/> class.
        /// </summary>
        /// <param name="currWeb">The curr web.</param>
        /// <param name="nodeXml">The node XML.</param>
        public ESPPage(SPWeb currWeb, XmlReader nodeXml)
        {
            if (nodeXml.Read())
            {
                PublishingWeb myPublishingWeb               = PublishingWeb.GetPublishingWeb(currWeb);
                PublishingPageCollection myPublishingPages  = myPublishingWeb.GetPublishingPages();

                #region Reading new page atributes

                string name                 = nodeXml.GetAttribute("Name");
                string pagelayout           = nodeXml.GetAttribute("PageLayout");
                string status               = nodeXml.GetAttribute("Status");
                string title                = nodeXml.GetAttribute("Title");
                string includeInNavigation  = nodeXml.GetAttribute("IncludeInNavigation");

                PageLayout[] myPageLayouts = myPublishingWeb.GetAvailablePageLayouts();
                PageLayout myPageLayout = null;

                for (int i = 0; i < myPageLayouts.Length; i++)
                {
                    if (myPageLayouts[i].Name == pagelayout)
                    {
                        myPageLayout = myPageLayouts[i];
                        break;
                    }
                }
                
                #endregion
              
                #region Finding Page

                PublishingPage myPublishingPage = myPublishingPages["Pages/" + name];
                
                #endregion

                if (myPublishingPage != null) 
                {
                    #region Change Page Fields
                    try
                    {
                        myPublishingPage.CheckOut();
                        //myPublishingPage.Title = title;

                        if (myPageLayout != null)
                        {
                            myPublishingPage.Layout = myPageLayout;
                        }

                        if ( includeInNavigation.Equals( "false" ) )
                        {
                            myPublishingPage.IncludeInCurrentNavigation = false;
                        }
                        else
                        {
                            myPublishingPage.IncludeInCurrentNavigation = true;
                        }

                        myPublishingPage.Update();

                        string checkInComment = "Changed by Millenet Power Tool";
                        
                        myPublishingPage.CheckIn( checkInComment );
                        
                        SPFile pageFile = myPublishingPage.ListItem.File;
                        pageFile.Publish( checkInComment );
                        pageFile.Approve( checkInComment );

                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error in Page {0}: {1}", name, e.Message);
                    }

                    #endregion
                }
                else
                {
                    #region Create New Page
                    
                    myPublishingPage = myPublishingWeb.GetPublishingPages().Add(name, myPageLayout);
                    myPublishingPage.Title = title;
                    myPublishingPage.Update();
                    myPublishingPage.CheckIn("Created by Millenet Power Tool");
                    myPublishingPage.Schedule();
                    
                    #endregion
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ESPPage"/> class.
        /// </summary>
        /// <param name="myPage">My page.</param>
        public ESPPage( PublishingPage myPage )
        {
            _page = myPage;
        }
    }
}
